package com.ibm.cps.servlet.starter;

import java.util.HashMap;
import java.util.Map;

import com.ibm.util.exception.CPSException;

public class CommandlineProcessMonitor implements ProcessMonitor {
	private Map<String, WorkerProcess> runningProcessors = new HashMap<>();

	@Override
	public boolean isAlive(String tenantId) throws CPSException {
		if(!runningProcessors.containsKey(tenantId)){
			return false;
		}else{
			WorkerProcess app = runningProcessors.get(tenantId);
			if(app.isAlive()){
				return true;
			}
			runningProcessors.remove(tenantId);
			return false;
		}

	}

	@Override
	public void registerStarted(String tenantId, WorkerProcess process)
			throws CPSException {
		runningProcessors.put(tenantId, process);
	}

	@Override
	public void stop(String tenantId) throws CPSException {
		if(isAlive(tenantId)){
			WorkerProcess process = runningProcessors.get(tenantId);
			process.stop();
		}
	}

}
